package simple.flow.common;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @author lhd
 */

@Data
public class RestResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "响应码")
    private int code;
    @ApiModelProperty(value = "响应信息")
    private String msg;
    @ApiModelProperty(value = "响应数据")
    private T data;
    @ApiModelProperty(value = "响应时间戳")
    private long timestamp = System.currentTimeMillis();

    public RestResult(final int code, final String msg, final T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <E> RestResult<E> success(final int code, final E data, final String msg) {
        return new RestResult<>(code, msg, data);
    }

    public static <E> RestResult<E> success(final E data) {
        return new RestResult<>(200, "success", data);
    }

    public static RestResult<Object> success() {
        return new RestResult<>(200, "success", "");
    }

    public static RestResult<Object> error(final int code, final String msg) {
        return new RestResult<>(code, msg, "");
    }

    public static RestResult<Object> error(final String msg) {
        return new RestResult<>(-1, msg, null);
    }
}
